package cb.com.etcdemo.http;

import android.util.Log;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.Map;

import cb.com.etcdemo.util.Tools;
import okhttp3.Call;

/**
 * Created by cb on 2018/4/7.
 */

public class Okhttp {


    public static void post(String url, Map<String, String> paramters,
                            final Objectcallback callback) {
        try {
            OkHttpUtils
                    .post()
                    .url(url)
                    .params(paramters)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e("888", "失败！");
                            callback.onFalia(id, e.toString());


                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                callback.onsuccess(responseString);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void get(String url, Map<String, String> paramters,
                           final Objectcallback callback) {
        try {
            OkHttpUtils
                    .get()
                    .url(url)
                    .params(paramters)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callback.onFalia(id, e.toString());
                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                callback.onsuccess(responseString);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
   public static void postForm(String url, Map<String, String> paramters, File file,
                               final Objectcallback callback){


//       Map<String, String> headers = new HashMap<>();
//       headers.put("Content-Disposition", "form-data;filename=enctype");
//       PostFormBuilder builder = OkHttpUtils.post();
//       builder.url(url);
//       for (int i = 0; i < files.size(); i++) {
//           String filename = files.get(i).getName();
//           builder.addFile("mFile", filename, files.get(i));
//       }
//
//       builder.params(paramters)
//               .headers(headers)
//               .build()
//               .execute(new StringCallback() {
//                   @Override
//                   public void onError(Call call, Exception e, int id) {
//                       callback.onFalia(id, e.toString());
//                   }
//
//                   @Override
//                   public void onResponse(String response, int id) {
//                       if (callback != null) {
//                           callback.onsuccess(response);
//                       }
//                   }
//               });



       OkHttpUtils.post()
               .addFile("photo", Tools.getUUID32()+".jpg", file)
               .url(url)
               .params(paramters)
//               .headers(headers)//
               .build()
               .execute(new StringCallback() {
                   @Override
                   public void onError(Call call, Exception e, int id) {
                       callback.onFalia(id, e.toString());
                   }

                   @Override
                   public void onResponse(String response, int id) {
                       if (callback != null) {
                           callback.onsuccess(response);
                       }
                   }
               });
   }
    public interface Objectcallback {
        void onsuccess(String st);

        void onFalia(int code, String errst);

    }


}
